//
//  RGSearchView.m
//  RedGrass
//
//  Created by wukong on 2019/2/13.
//  Copyright © 2019年 hongcaosp. All rights reserved.
//

#import "RGSearchView.h"
#import "RGSearchToolBarView.h"
#import "RGSearchSlideView.h"

@interface RGSearchView()<RGSearchSlideViewDelegate,UIScrollViewDelegate>

@property (strong, nonatomic) RGSearchToolBarView *toolBarView;
@property (strong, nonatomic) RGSearchSlideView *slideView;
//@property (strong, nonatomic) UIScrollView *containerView;


@end

@implementation RGSearchView

-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self initSubView];
    }
    return self;
}

-(void) initSubView{
    [self addSubview:self.toolBarView];
    [_toolBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).mas_offset(40);
        make.left.and.right.mas_equalTo(self).mas_offset(0);
        make.height.mas_equalTo(50);
    }];
    
    [self addSubview:self.slideView];
    [_slideView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.mas_equalTo(self).mas_offset(0);
        make.top.mas_equalTo(self.toolBarView.mas_bottom).mas_offset(0);
        make.height.mas_equalTo(45);
    }];
    
    [self addSubview:self.containerView];
    [_containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.mas_equalTo(self).mas_offset(0);
        make.top.mas_equalTo(self.slideView.mas_bottom).mas_offset(0);
        make.bottom.mas_equalTo(self).mas_offset(0);
    }];
}

#pragma mark - slideView delegate

-(void)onTabTapAction:(NSInteger)index{
    if([self.viewDelegate respondsToSelector:@selector(changeSearchResultUIWithView:tag:)]){
        [self.viewDelegate changeSearchResultUIWithView:self tag:index];
    }
}

#pragma mark - public
-(void) becomFirstReponse{
    [self.toolBarView.searchBar becomeFirstResponder];
}

#pragma mark - getter
-(RGSearchToolBarView *)toolBarView{
    if(!_toolBarView){
        _toolBarView = [[RGSearchToolBarView alloc]init];
        AA_WEAK_MAKE(self)weakSelf = self;
        [_toolBarView setBackBtnClickBlock:^{
            if([weakSelf.viewDelegate respondsToSelector:@selector(backWithView:)]){
                [weakSelf.viewDelegate backWithView:weakSelf];
            }
        }];
        
        [_toolBarView setSearchBtnClickBlock:^(NSString * _Nonnull text) {
            if([weakSelf.viewDelegate respondsToSelector:@selector(searchVedioAndUserWithView:text:)]){
                [weakSelf.viewDelegate searchVedioAndUserWithView:weakSelf text:text];
            }
        }];
    }
    return _toolBarView;
}

-(RGSearchSlideView *)slideView{
    if(!_slideView){
        _slideView = [[RGSearchSlideView alloc]init];
        _slideView.delegate = self;
        [_slideView setLabels:@[@"视频",@"用户"] tabIndex:0];
    }
    return _slideView;
}

-(UIScrollView *)containerView{
    if(!_containerView){
        _containerView = [[UIScrollView alloc]init];
//        _containerView.contentSize = CGSizeMake(self.containerView.frame.size.width * 2, 0);
        _containerView.pagingEnabled = YES;
        _containerView.scrollEnabled = NO;
        _containerView.delegate = self;
        _containerView.showsHorizontalScrollIndicator = NO;
        _containerView.showsVerticalScrollIndicator = NO;
    }
    return _containerView;
}

@end
